//
//  ReadListViewController.m
//  Leisure
//
//  Created by xalo on 16/6/29.
//  Copyright © 2016年 Eason. All rights reserved.
//

#import "ReadListViewController.h"

@interface ReadListViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong)UISegmentedControl *segment;
//最新列表
@property (weak, nonatomic) IBOutlet UITableView *newestTableView;
//最热列表
@property (weak, nonatomic) IBOutlet UITableView *hotTableView;
//滚动视图
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;


/** 最新列表起始位置 */
@property (nonatomic,assign)NSInteger newStart;
/** 最新列表的数据的条数 */
@property (nonatomic,assign)NSInteger newLimit;
/** 最热列表的数据起始位置 */
@property (nonatomic,assign)NSInteger hotStart;
/**  最热列表的数据的条数*/
@property (nonatomic,assign)NSInteger hotLimit;


@property(nonatomic,strong)NSMutableArray *newestDataSource;

@property (nonatomic,strong)NSMutableArray *hotDataSource;


@end

@implementation ReadListViewController


- (NSMutableArray *)newestDataSource{
    if (!_newestDataSource) {
        _newestDataSource  = [NSMutableArray array];
    }
    return _newestDataSource;
}
- (NSMutableArray *)hotDataSource{
    if (!_hotDataSource) {
        _hotDataSource = [NSMutableArray array];
    }
    return _hotDataSource;
}

- (UISegmentedControl *)segment{
    
    if (!_segment) {
        _segment = [[UISegmentedControl alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
//        插入标签
        [_segment insertSegmentWithTitle:@"最新" atIndex:0 animated:YES];
        [_segment insertSegmentWithTitle:@"最热" atIndex:1 animated:YES];
        [_segment addTarget:self action:@selector(clickSegmentedController:) forControlEvents:UIControlEventValueChanged];
        
    }
    return _segment;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor purpleColor];
    self.navigationItem.titleView = self.segment;
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.newestTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.hotTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
//    数据请求
    [self loadNewestTableViewDataSourceWithStart:0 limit:15];
    [self loadHotTableViewFDataSourceWithStart:0 limit:15];
}

//segment点击事件
- (void)clickSegmentedController:(UISegmentedControl *)sender{
    if (sender.selectedSegmentIndex == 0) {
        [self.scrollView setContentOffset:CGPointZero animated:YES];
    }else{
        [self.scrollView setContentOffset:CGPointMake(kWidth, 0) animated:YES];
    }
    
}
#pragma mark ------------------数据请求--------------------------
//加载最新列表的数据
//@(self.typeID) 参数必须转换成NSInNumber类型
- (void)loadNewestTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fectDataWithUrl:kReadListUrl parameter:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"addtime"} method:@"POST" compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        NSLog(@"%@",bigDic);
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            ReadListModel *list = [[ReadListModel alloc]init];
            [list setValuesForKeysWithDictionary:smallDic];
            [self.newestDataSource addObject:list];
        }
        
        
        
    } upDateUI:^{
        
        [self.newestTableView reloadData];
        
    }];
}
//加载最热列表的数据
- (void)loadHotTableViewFDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fectDataWithUrl:kReadListUrl parameter:@{@"typeid":@(self.typeID),@"start":@(start),@"limit":@(limit),@"sort":@"hot"} method:@"POST" compelet:^(NSData *data) {
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *smallDic in bigDic[@"data"][@"list"]) {
            ReadListModel *list = [[ReadListModel alloc]init];
            [list setValuesForKeysWithDictionary:smallDic];
            [self.hotDataSource addObject:list];
        }
        
        
    } upDateUI:^{
        
        [self.hotTableView reloadData];
        
    }];
}

#pragma mark -----------------------ScrollViewDelegate-----------------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    //    判断滚动的是不是你要滚的
    if (scrollView == self.scrollView) {
        if (self.scrollView.contentOffset.x == 0) {
            self.segment.selectedSegmentIndex =0;
        }else if (self.scrollView.contentOffset.x == kWidth){
            self.segment.selectedSegmentIndex = 1;
        }
    }
}



#pragma mark -----------------------------TableViewDelegate---------------------------
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (tableView == self.newestTableView) {
        return self.newestDataSource.count;
    }else{
        return self.hotDataSource.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
   
    
//    创建一个Model来接收
    ReadListModel *model = [[ReadListModel alloc]init];
    if (tableView == self.newestTableView) {
        model = self.newestDataSource[indexPath.row];
        
    }else{
        model = self.hotDataSource[indexPath.row];
        
    }
     cell.textLabel.text = model.title;
    return cell;
    
}






@end
